package spark.kafka;

import org.apache.kafka.common.serialization.Serializer;
import spark.sparkStreaming.ValueUpdateStatus;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;

/**
 * 自定义序列化类
 */
public class MySerializer implements Serializer<ValueUpdateStatus>{

    public void configure(Map<String, ?> configs, boolean isKey) {

    }

    public byte[] serialize(String topic, ValueUpdateStatus data) {
        byte[] bytes = null;
        if(data == null){
            return  null;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);// 对象流
            objectOutputStream.writeObject(data); // 写出
            bytes = byteArrayOutputStream.toByteArray(); // 返回
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
    public void close() {

    }
}
